package com.briup.product_source.controller;

import com.briup.product_source.exception.ServiceException;
import com.briup.product_source.pojo.ManagerFenceHouse;
import com.briup.product_source.result.Result;
import com.briup.product_source.service.FenceHouseService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "栏舍管理模块")
@RestController
@RequestMapping("/fenceHouse")
public class FenceHouseController {
    @Autowired
    private FenceHouseService fenceHouseService;
    @ApiOperation("分页多条件查询栏舍信息接口")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "pageNum",value = "当前页码", defaultValue = "1", required = true),
    @ApiImplicitParam(name = "pageSize",value = "每页大小", defaultValue = "10", required = true),
    @ApiImplicitParam(name = "fhName",value = "栏舍名称")
})
    @GetMapping
    public Result queryByConditionsAndPage
            (Integer pageNum, Integer pageSize, String fhName) {
        PageInfo<ManagerFenceHouse> pageInfo =
                fenceHouseService.findByPage(pageNum, pageSize, fhName);
        return Result.success(pageInfo);
    }
    // 新增：根据栏舍ID查询栏舍及栏圈信息接口
    @GetMapping("{fhId}")
    @Operation(summary = "查询指定栏舍(含栏圈)")
    @Parameter(name ="fhId" ,description = "栏舍id",in = ParameterIn.PATH)
    public Result getByIdWithMhs(@PathVariable String fhId){
        return Result.success(fenceHouseService.getDetailByFhId(fhId));
    }
    @PostMapping("/saveOrUpdate")
    @Operation(summary = "新增/修改栏舍数据")
    public Result saveOrUpdate(@RequestBody ManagerFenceHouse managerFenceHouse){
        // 因为可能出现异常，需要处理一下错误返回
        try {
            fenceHouseService.saveOrUpdate(managerFenceHouse);
            return Result.success();
        }catch (ServiceException e){
            return Result.failure(e.getResultCode());
        }
    }
    @DeleteMapping("/{fhId}")
    @Operation(summary = "根据栏舍编号删除栏舍信息接口")
    public Result deleteById(@PathVariable String fhId){
        try {
            fenceHouseService.deleteById(fhId);
            return Result.success();
        }catch (ServiceException e){
            return Result.failure(e.getResultCode());
        }
    }
    @DeleteMapping("/deleteByIdAll")
    @Operation(summary = "批量删除栏舍")
    public Result removeByIdAll(@RequestBody List<String> ids){
        try {
            fenceHouseService.removeByIdAll(ids);
            return Result.success();
        }catch (ServiceException e){
            return Result.failure(e.getResultCode());
        }
    }
    @GetMapping("/queryAll")
    @Operation(summary = "查询所有栏舍")
    public Result queryAll(){
        return Result.success(fenceHouseService.getAll());
    }



}